﻿using System;
using System.Net;
using System.Runtime.InteropServices.Automation;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using XamlingCore.Infrastructure.Elevated;

namespace XamlingCore.Infrastructure.OOB
{
    public static class WindowsStartup
    {
        public static bool SetStartup()
        {
            if (Application.Current.IsRunningOutOfBrowser && Application.Current.HasElevatedPermissions && AutomationFactory.IsAvailable)
            {
                using (dynamic ShellApplication = AutomationFactory.CreateObject("Shell.Application"))
                {
                    dynamic commonPrograms = ShellApplication.NameSpace(11);
                    string allUsersPath = commonPrograms.Self.Path;

                    dynamic directory = ShellApplication.NameSpace(allUsersPath + @"\Programs");
                    if (directory != null)
                    {
                        dynamic link = directory.ParseName(Deployment.Current.OutOfBrowserSettings.ShortName + ".lnk");
                        if (link != null)
                        {
                            if (FileSystem.Exists(link.Path))
                            {
                                string OOBLink = link.Path;
                                var target = allUsersPath + @"\Programs\Startup\" + System.IO.Path.GetFileName(OOBLink);

                                FileSystem.Copy(OOBLink, target);
                                return true;
                            }
                            else
                            {
                                return false;
                            }


                        }
                    }
                }
            }
            return false;
        }
        public static void RemoveStartup()
        {
            if (Application.Current.IsRunningOutOfBrowser && Application.Current.HasElevatedPermissions && AutomationFactory.IsAvailable)
            {
                using (dynamic ShellApplication = AutomationFactory.CreateObject("Shell.Application"))
                {
                    dynamic commonPrograms = ShellApplication.NameSpace(11);
                    string allUsersPath = commonPrograms.Self.Path;

                    dynamic directory = ShellApplication.NameSpace(allUsersPath + @"\Programs");
                    if (directory != null)
                    {
                        dynamic link = directory.ParseName(Deployment.Current.OutOfBrowserSettings.ShortName + ".lnk");
                        if (link != null)
                        {
                            string OOBLink = link.Path;
                            var target = allUsersPath + @"\Programs\Startup\" + System.IO.Path.GetFileName(OOBLink);
                            FileSystem.Delete(target);
                        }

                    }

                }
            }
        }

        public static bool StartupSet
        {
            get
            {
                if (Application.Current.IsRunningOutOfBrowser && Application.Current.HasElevatedPermissions)
                {


                    using (dynamic ShellApplication = AutomationFactory.CreateObject("Shell.Application"))
                    {
                        dynamic commonPrograms = ShellApplication.NameSpace(11);
                        string allUsersPath = commonPrograms.Self.Path;

                        dynamic directory = ShellApplication.NameSpace(allUsersPath + @"\Programs");
                        if (directory != null)
                        {
                            dynamic link =
                                directory.ParseName(Deployment.Current.OutOfBrowserSettings.ShortName + ".lnk");
                            if (link == null)
                            {
                                return false;
                            }
                            string OOBLink = link.Path;
                            var target = allUsersPath + @"\Programs\Startup\" + System.IO.Path.GetFileName(OOBLink);
                            return FileSystem.Exists(target);
                        }
                    }
                }
                return false;
            }
        }

    }
}
